﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.AppCode.Lib;

namespace mojoPortal.Web.RS.Modules.RFQ.Controls
{
    public partial class PublicRFQListControl : UserControl // RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        public void LoadPublicRFQ()
        {
            LoadIndustryType();
        }

        private bool LoadIndustryType()
        {
            bool bHasValue = false;

            SqlCommand myCommand;
            SqlConnection myConnection;
            String sqlSELECT = "select t.* from rs_tblType t where t.category='INDUSTRY_CAT' and t.typeid in(select r.industrytype from rs_tblrfq r where r.isapproved=1 and r.isconfirmed=1 and r.isclosed=0) order by t.typename asc";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            gvIndustryType.DataSource = ds;
            gvIndustryType.DataBind();
            myConnection.Close();

            if (gvIndustryType.Rows.Count > 0)
            {
                bHasValue = true;
            }

            return bHasValue;
        }

        private DataSet GetChildListDataSet(int industrytypeid)
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;

            String sqlSELECT = "select * from rs_tblRFQ where industrytype=@industrytypeid";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ReadyServiceConnectionStringMain"].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@industrytypeid";
            param.Value = industrytypeid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);

            return ds;
        }
        protected void gvIndustryType_OnRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView gv = (GridView)e.Row.FindControl("gvRequest");
                if (null != gv && gv is GridView)
                {
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    int industrytypeid = Convert.ToInt32(drv["TypeID"]);
                    gv.DataSource = GetChildListDataSet(industrytypeid);
                    gv.DataBind();
                }
            }
        }
        protected void gvRequest_Edit(object sender, CommandEventArgs e)
        {
            int rfqid = Convert.ToInt32(e.CommandArgument.ToString());
            if (rfqid > 0)
            {
                ucModalPublicRFQControl.ShowModal(rfqid);
            }
        }

        protected void btnAddRFQ_Click(object sender, EventArgs e)
        {
            ucModalPublicRFQControl.ShowModal(0);
        }
    }
}
